using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class CachDungDAO
    {
        public static bool Insert(CachDungDTO cd)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlParam = new List<SqlParameter>();
                sqlParam.Add(new SqlParameter("@MaCachDung", cd.MaCachDung));
                sqlParam.Add(new SqlParameter("@TenCachDung", cd.TenCachDung));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spCachDungInsert", sqlParam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Delete(CachDungDTO cd)
        {
            bool kq = false;

            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaCachDung", cd.MaCachDung));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spCachDungDelete",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static List<CachDungDTO> Select()
        {
            List<CachDungDTO> lst = new List<CachDungDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spCachDungSelect");
                foreach (DataRow dr in dt.Rows)
                {
                    CachDungDTO cd = new CachDungDTO();
                    cd.MaCachDung = int.Parse(dr["MaCachDung"].ToString());
                    cd.TenCachDung = (dr["TenCachDung"].ToString());
                    lst.Add(cd);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lst;

        }
        public static CachDungDTO SelectByID(int MaCachDung)
        {
            CachDungDTO cd = new CachDungDTO();

            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaCachDung",MaCachDung));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spCachDungSelectByID", sqlparam);
                DataRow dr = dt.Rows[0];
                cd.MaCachDung = int.Parse(dr["MaCachDung"].ToString());
                cd.TenCachDung = (dr["TenCachDung"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return cd;
        }
        public static bool Update(CachDungDTO cd)
        {
            bool kq = false;

            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaCachDung",cd.MaCachDung));
                sqlparam.Add(new SqlParameter("@TenCachDung", cd.TenCachDung));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spCachDungUpdate", sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
    }
}
